package com.px.task.service.impl;

import com.px.common.utils.file.FileUploadUtils;
import com.px.device.domain.RoutesInfo;
import com.px.device.mapper.RoutesInfoMapper;
import com.px.device.domain.AirportInfo;
import com.px.device.service.IAirportInfoService;
import com.px.task.domain.TaskCalendar;
import com.px.task.domain.TaskRecord;
import com.px.task.mapper.TaskRecordMapper;
import com.px.task.service.ITaskCalendarService;
import com.px.task.service.ITaskRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 任务记录Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Slf4j
@Service
public class TaskRecordServiceImpl implements ITaskRecordService 
{
    @Autowired
    private TaskRecordMapper taskRecordMapper;
    @Autowired
    private ITaskCalendarService taskCalendarService;
    @Autowired
    private RoutesInfoMapper routesInfoMapper;
    @Autowired
    private IAirportInfoService airportInfoService;

    /**
     * 查询任务记录
     * 
     * @param id 任务记录主键
     * @return 任务记录
     */
    @Override
    public TaskRecord selectTaskRecordById(Long id)
    {
        return taskRecordMapper.selectTaskRecordById(id);
    }

    /**
     * 查询任务记录列表
     * 
     * @param taskRecord 任务记录
     * @return 任务记录
     */
    @Override
    public List<TaskRecord> selectTaskRecordList(TaskRecord taskRecord)
    {
        taskRecord.setDelFlag("0");
        return taskRecordMapper.selectTaskRecordList(taskRecord);
    }

    /**
     * 新增任务记录
     * 
     * @param taskRecord 任务记录
     * @return 结果
     */
    @Override
    public int insertTaskRecord(TaskRecord taskRecord)
    {
        return taskRecordMapper.insertTaskRecord(taskRecord);
    }

    /**
     * 修改任务记录
     * 
     * @param taskRecord 任务记录
     * @return 结果
     */
    @Override
    public int updateTaskRecord(TaskRecord taskRecord)
    {
        return taskRecordMapper.updateTaskRecord(taskRecord);
    }

    /**
     * 批量删除任务记录
     * 
     * @param ids 需要删除的任务记录主键
     * @return 结果
     */
    @Override
    public int deleteTaskRecordByIds(Long[] ids)
    {
        return taskRecordMapper.deleteTaskRecordByIds(ids);
    }

    /**
     * 删除任务记录信息
     * 
     * @param id 任务记录主键
     * @return 结果
     */
    @Override
    public int deleteTaskRecordById(Long id)
    {
        return taskRecordMapper.deleteTaskRecordById(id);
    }

    @Override
    public void insertTaskRecordByCalendar(Long taskCalendarId) {
        TaskRecord record = new TaskRecord();

        TaskCalendar taskCalendar = taskCalendarService.selectTaskCalendarById(taskCalendarId);
        Assert.notNull(taskCalendar, "错误的任务ID,ID=" + taskCalendarId);
        RoutesInfo routesInfo = routesInfoMapper.selectRoutesInfoByRoutesId(taskCalendar.getWayPointId());
        Assert.notNull(routesInfo, "错误的航线ID,ID=" + taskCalendar.getWayPointId());
        AirportInfo airportInfo = airportInfoService.selectAirportInfoByAirportId(routesInfo.getAirportId());

        record.setCalendarId(taskCalendarId);
        record.setTaskName(taskCalendar.getTaskName());
        record.setTaskDesc(taskCalendar.getTaskDesc());
        record.setRoutesName(routesInfo.getRoutesName());
        record.setRoutesUrl(routesInfo.getRoutesUrl());
        record.setAirportId(routesInfo.getAirportId());
        record.setAirportName(airportInfo.getAirportName());
        record.setAirportSn(airportInfo.getAirportSn());
        record.setAirportAddress(airportInfo.getAirportAddress());
        record.setAirportChildName(airportInfo.getAirportChildName());
        record.setTaskBeginTime(new Date());
        record.setTaskStatus(0);
        record.setRoutesStartPoint(routesInfo.getRoutesStartPoint());
        record.setRoutesEndPoint(routesInfo.getRoutesEndPoint());
        record.setRouteMileage(routesInfo.getRouteMileage());
        try {
            String routeImageUrl = FileUploadUtils.copyFromPreviewUrl(routesInfo.getRoutesUrl());
            record.setRoutesUrl(routeImageUrl);
        } catch (Exception e) {
            log.error("复制航线预览图失败,启用原航线图片!预览图地址:" + routesInfo.getRoutesUrl());
            record.setRoutesUrl(routesInfo.getRoutesUrl());
        }
        record.setCreateBy(taskCalendar.getCreateBy());
        record.setDeptId(taskCalendar.getDeptId());
        insertTaskRecord(record);

    }

    @Override
    public List<Map<String, Object>> selectTrDayCount(int day) {

        return taskRecordMapper.selectTrDayCount(day-1);
    }

    public static void main(String[] args) {


    }
}
